
var token = sessionStorage.getItem("token");
if (!token) {
    console.log(1)
    location.href = "http://114.115.133.220/account/src/login/login.html";
}
else {
    var account = [];
    function init() {
        $.ajax({
            type: "GET",
            url: "http://114.115.133.220:2023/api/account",
            headers: {
                token: token,
            },
            async: false,
            success: function (res) {
                account = res.data;
                var html = "";
                account.forEach(item => {
                    html += `
                <div class="panel panel-success">
                  <div class="panel-heading"><span>${formatDateTime(new Date(item.time))}</span>  <span data-id =${item._id} class='account-update' style='margin-left:209px'>修改</span></div>
                  <div class="panel-body">
                    <div class="account-describe">${item.title}</div>
                    <div class=" text-center">
                      <span class='label ${item.type === 1 ? "label-success" : "label-error"}'>${item.type === 1 ? "收入" : "支出"}</span>
                    </div>
                    <div class="text-right sum">${item.account}元</div>
                    <div class="text-right">
                      <span data-id =${item._id} class="label label-remove remove">删除</span>
                    </div>
                  </div>
                </div>
                `
                });
                $(".accounts").html(html);
            }
        })
    }


    // 点击删除

    $(".accounts").on("click", ".remove", function () {
        if (confirm("是否删除?")) {
            var id = $(this).attr("data-id");
            $.ajax({
                type: "delete",
                url: `http://114.115.133.220:2023/api/delete/${id}`,
                headers: {
                    token: token,
                },
                async: false,
                success: function (res) {
                    if (res.code === "10000") {
                        init();
                    }
                    else {
                        alert(res.message);
                    }
                }
            })
        }
    })

    // 点击修改弹出修改弹窗
    $(".accounts").on("click", ".account-update", function () {
        popup(".pop");
        $("#item").val($(this).parent(".panel-heading").next().find(".account-describe").text());
        $("#time").val($(this).prev().text());
        $("#type").val($(this).parent(".panel-heading").next().find(".label").hasClass("label-error") ? "-1" : "1");
        var sum = $(this).parent(".panel-heading").next().find(".sum").text();
        var index = sum.indexOf("元");
        $("#account").val(sum.substring(0, index));
    })
    //点击立即修改 
    $(".add-submit").click(function () {
        var id = $(".account-update").attr("data-id");
        const itemVal = $("#item").val(),
            timeVal = $("#time").val(),
            accountVal = $("#account").val(),
            typeVal = $("#type").val(),
            remarksVal = $("#remarks").val(),
            userIdVal = sessionStorage.getItem("id") || "";
        if (!itemVal || !timeVal || !accountVal || !remarks ||!remarksVal) {
            return alert("有一项为空");
        }
        $.ajax({
            type: "patch",
            url: `http://114.115.133.220:2023/api/update/${id}`,
            headers: {
                token: token
            },
            data: {
                title: itemVal,
                time: timeVal,
                type: Number(typeVal),
                account: Number(accountVal),
                remarks: remarksVal,
                userId: userIdVal
            },
            success: function (res) {
                if (res.code === "10000") {
                    alert("修改成功");
                    init();
                    hideMask(".pop");
                }
                else {
                    console.log(res.message);
                }   
            }
        })
    })

    init();

    // 转化时间 2022-10-10 10:10:10
    function formatDateTime(now) {
        now = now ? new Date(now) : new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        // var hour = now.getHours();
        // var minute = now.getMinutes();
        // var second = now.getSeconds();
        return year + "-" + formatTwo(month) + "-" + formatTwo(date);
    }

    function formatTwo(n) {
        n = n.toString();
        return n[1] ? n : "0" + n;
    }
}


